// kanji.cxx
//
// Copyright (C) 2011, 2012 - Massimiliano Maniscalco
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "kanji.h"


bool Kanji::
is_kanji ( const Glib::ustring &str )
{
	return ( str.size() == 1 ) && ( (str[0] >= 0x4E00 && str[0] <= 0x9FCF) || (str[0] >= 0x3400 && str[0] <= 0x4DBF) || (str[0] >= 0x20000 && str[0] <= 0x2A6DF) || (str[0] >= 0x2A700 && str[0] <= 0x2B73F) || (str[0] >= 0x2B740 && str[0] <= 0x2B81F) || (str[0] >= 0x2F800 && str[0] <= 0x2FA1F) || (str[0] >= 0xF900 && str[0] <= 0xFAFF) ) ? true : false;
}

void Kanji::
parse_letture ( const Glib::ustring &str, list<Glib::ustring> &listStr )
{
	size_t _inizio = 0;
	size_t _fine = str.length();
	while ( _inizio < _fine )
	{
		size_t _pos = str.find_first_of ( " ,　、", _inizio );
		if ( _pos != Glib::ustring::npos )
		{
			Glib::ustring trovato = str.substr( _inizio, _pos - _inizio );				
			if ( trovato != "" )
				listStr.push_back ( trovato );
			_inizio = _pos + 1;
		}
		else
		{
			Glib::ustring trovato = str.substr( _inizio );
			if ( trovato != "" )
				listStr.push_back ( trovato );
			_inizio = _fine;
		}
	}
}

void Kanji::
create_string ( const list<Glib::ustring> &listStr, Glib::ustring &str ) const
{
	if ( listStr.size() > 0 )
	{
		list<Glib::ustring>::const_iterator _iter = listStr.begin ();
		list<Glib::ustring>::const_iterator _end = listStr.end ();
		str = *_iter;
		_iter++;
		while ( _iter != _end )
		{
			str += ", " + *_iter;
			_iter++;
		}
	}
}